<?php
class EnquireController extends Apps_Controller_Action_Default {
	public function indexAction() {
        $rq   = $this->_request;
        $id   = intval($rq->getParam('id'));
        $type = intval($rq->getParam('t'));

        if(($id && !$type) || (!$id && $type) || !in_array($type, array(1,2,3))) {
            $redirectUrl = "/enquire/index";
        } else {
        	$redirectUrl = "/enquire/index/id/". $id ."/t/" . $type;
        }

        $f = new F_Enquire();
        if($rq->isPost()) {
            if($f->isValid($rq->getParams())) {
                $fullName = $f->getValue('full_name');
                $emailCustomer = $f->getValue('email');
                $reEmail  = $f->getValue('reconfirm_email');
                $phone    = $f->getValue('phone');
                $subject  = $f->getValue('subject');
                $inquyry  = $f->getValue('inquyry');
                $captcha  = $f->getValue('captcha');
                $countryResidence = $f->getValue('country_residence');

                if($emailCustomer != $reEmail) {
                    $this->view->error = "Reconfirm_email_doesn't_match";
                } else {
                    //check captcha
                    $captchaSession = new Zend_Session_Namespace('captchaSession');
                    $captchaCodeSession = $captchaSession->captcha_code;

                    $captchaCode = md5($captcha . '760211');
                    if($captchaCodeSession != $captchaCode) {
                        $this->view->error = "Captcha_doesn't_match";
                    } else {
                        $data = array(
                                'reference_id'          => $id,
                                'reference_type'        => $type,
                                'full_name'             => $this->quote($fullName),
                                'email'                 => $this->quote($emailCustomer),
                                'phone'                 => $this->quote($phone),
                                'country_residence'     => $this->quote($countryResidence),
                                'subject'               => $this->quote($subject),
                                'message'               => $this->quote($inquyry)
                                );

                        $tbEnquire = new M_DbTable_Enquire();
                        $enquireId = $tbEnquire->insert($data);
                        if($enquireId) {
                        	//SEND EMAIL TO SALES
                        	$tbVctSetting = new M_DbTable_VctSetting();
                        	$vctSetting   = $tbVctSetting->getVctSetting()->query()->fetch();
                        	
                        	$passwordEmailVct = $vctSetting['password_email'];
                        	$passwordEmailVct = substr(substr($passwordEmailVct, 8), 0, -7); //decode
                        	
                        	$mail = new Apps_PhpMailer();
                        	$mail->IsSMTP();
                        	$mail->IsHTML(true);
                        	$mail->SMTPDebug = 2;
                        	
                        	try {
                        		$mail->SMTPAuth   = true;
                        		$mail->CharSet    = 'UTF-8';
                        		$mail->Host       = $vctSetting['host_email'];;
                        		$mail->Port       = $vctSetting['port_email'];
                        		$mail->SMTPSecure = $vctSetting['ssl_email'];
                        		$mail->Username   = $vctSetting['email'];
                        		$mail->Password   = $passwordEmailVct;
                        	
                        		$inquyry .= "<table style='margin-top:30px' border='1px' cellpadding='10'>
                        						<tr>
                        							<td colspan='2'><b>Customer Information</b></td>
                        						</tr>
                        						<tr>
                        							<td><b>Full Name</b></td>
                        							<td>". $fullName ."</td>
                        						</tr>
                        						<tr>
                        							<td><b>Email</b></td>
                        							<td>". $emailCustomer ."</td>
                        						</tr>
                        						<tr>
                        							<td><b>Phone</b></td>
                        							<td>". $phone ."</td>
                        						</tr>
                        						<tr>
                        							<td><b>Country Of Residence</b></td>
                        							<td>". $countryResidence ."</td>
                        						</tr>
                        					</table>";  
                        	
                        		$title = 'detail';
                        		if($type == 1) {
                        			$tourModel = new M_Tour();
                        			$tour = $tourModel->getTourById($id)->query()->fetch();
                        			$title = trim(str_replace(' ', '-', $tour['title']));
                        			$inquyry = $inquyry . '<br/><br/><a style="color:#F3951F;font-size:16px"
                        			href="http://vietcentertourist.com/tour/detail/'.$id.'-'.$title .'.html">
                        			http://vietcentertourist.com/tour/detail/'.$id.'-'.$title .'.html</a>';
                        		} else if($type == 2) {
                        			$cruiseModel = new M_Cruise();
                        			$cruise = $cruiseModel->getCruiseInfo($id)->query()->fetch();
                        			$title = trim(str_replace(' ', '-', $cruise['name']));
                        			$inquyry = $inquyry  . '<br/><br/><a style="color:#F3951F;font-size:16px"
                        			href="http://vietcentertourist.com/cruise/detail?id='. $id .'>
                        			http://vietcentertourist.com/cruise/detail/'.$id.'-'.$title .'.html </a>';
                        		}
                        	
                        		$mail->SetFrom($vctSetting['email'], $fullName);
                        		$mail->AddReplyTo($emailCustomer, $fullName);
                        	
                        		$mail->Subject = $subject;
                        		$mail->Body    = $inquyry;
                        	
                        		$validator = new Zend_Validate_EmailAddress();
                        		
                        		//GET SALES EMAIL
                        		$tbVctInfo = new M_DbTable_VctInfo();
                        		$vctInfo   = $tbVctInfo->getVctInfo()->query()->fetch();
                        		
                        		$toEmail   = $vctInfo['email_sales'] ? $vctInfo['email_sales'] : 'sales@vctourist.com';
                        		
                        		if($validator->isValid(trim($toEmail))) {
                        			try {
                        				$mail->AddAddress($toEmail);
                        				$mail->Send();
                        				$mail->ClearAddresses();
                        			} catch (Zend_Exception $e) {
                        				echo $e->getMessage();
                        			}
                        		}
                        	} catch (phpmailerException $e) {
                        		echo $e->errorMessage();
                        	} catch (Zend_Exception $e) {
                        		echo $e->getMessage();
                        	}
                        	
                        	$message = "Thank you! ". $fullName . '<br/>' . $vctSetting['reply_message_enquire'];
                            $this->_flashMessenger->addMessage($message);
                            $this->_redirect($redirectUrl);
                        } else {
                            $this->view->error = "Error!";
                        }
                    }
                }
            }
        }
        $this->view->form = $f;
	}
}